package com.example.androiddemo01.views;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.androiddemo01.R;
import com.example.androiddemo01.base.BaseFragment;
import com.example.androiddemo01.bean.Student;
import com.example.androiddemo01.database.DBHelper;
import com.example.androiddemo01.utils.SpUtils;

public class MineFragment extends BaseFragment implements View.OnClickListener {

   private TextView mTvName;
   private TextView mTvId;
   private TextView mTvCode;
   private TextView mTvUsername;
   private TextView mTvClass;
   private RelativeLayout mSettingLayout;

   private SpUtils mSpUtils;
   @Override
   protected int getLayoutId() {
      return R.layout.fragment_mine;
   }

   @Override
   protected void initView() {
      mSettingLayout = mView.findViewById(R.id.settingView);
      mTvName = mView.findViewById(R.id.my_name);
      mTvId = mView.findViewById(R.id.my_id);
      mTvCode = mView.findViewById(R.id.my_code);
      mTvUsername = mView.findViewById(R.id.username);
      mTvClass = mView.findViewById(R.id.class_name);
      mSpUtils = new SpUtils(mContext);
      int loginId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
      if (loginId >= 0) {
         Student student = DBHelper.getInstance(mContext).getStudentById(loginId);
         mTvName.setText(student.getStudent_name());
         mTvId.setText(String.valueOf(student.getStudent_id()));
         mTvCode.setText(student.getStudent_code());
         mTvUsername.setText(student.getUsername());
         mTvClass.setText(student.getClass_name());
      }
   }

   @Override
   protected void initListener() {
      mSettingLayout.setOnClickListener(this);
   }

   @Override
   public void onClick(View v) {
      int id = v.getId();
      if (id == R.id.settingView) {
         Intent intent = new Intent(mContext, SettingsActivity.class);
         startActivity(intent);
      }
   }
}
